using System.Collections.Generic;
using System.Collections.ObjectModel;
using System;
using Arere.Core.Data;

namespace Arere.Core
{
    /// <summary>
    /// A named holder for more areas. An
    /// area can be a city, district, building, etc.
    /// These areas hold other areas that they can
    /// connect to. They require an AreaAdapter to
    /// communicate and even be instantiated.
    /// </summary>
    public class Area
    {
        private AreaAdapter adapter;
        private Guid areaId;
        private string name;

        /// <summary>
        /// Create a new area connecting
        /// with an area adapter.
        /// </summary>
        /// <param name="adapter">The adapter
        /// that all of the properties connect to.</param>
        /// <param name="areaId">The unique identifier
        /// of the area as stored in the database.</param>
        /// <param name="name">The arbitrary name assigned
        /// to the area.</param>
        public Area(AreaAdapter adapter,
            Guid areaId, string name)
        {
            this.adapter = adapter;
            this.areaId = areaId;
            this.name = name;
        }

        /// <summary>
        /// Gets all of the areas that are
        /// child areas of this area. Uses
        /// the adapter to do so.
        /// </summary>
        public Area[] ChildAreas
        {
            get { return this.adapter.GetChildAreas(this.areaId); }
        }

        /// <summary>
        /// Add an area as a child to this one. Uses
        /// the current adapter that this object uses.
        /// </summary>
        /// <param name="childArea"></param>
        /// <returns></returns>
        public bool AddChildArea(Guid childArea)
        {
            return this.adapter.AddChildArea(
                this.areaId, childArea);
        }

        /// <summary>
        /// Removes a child area from the list
        /// of child areas for this object.
        /// </summary>
        /// <param name="childArea"></param>
        /// <returns></returns>
        public bool RemoveChildArea(Guid childArea)
        {
            return this.adapter.RemoveChildArea(
                this.areaId, childArea);
        }

        public Area[] AdjacentAreas
        {
            get { return this.adapter.GetAdjacentAreas(this.areaId); }
        }

        public bool AddAdjacentArea(Guid adjacentAreaId)
        {
            return this.adapter.AddAdjacentArea(
                this.areaId, adjacentAreaId);
        }

        public bool RemoveAdjacentArea(Guid adjacentAreaId)
        {
            return this.adapter.RemoveAdjacentArea(
                this.areaId, adjacentAreaId);
        }

        public Guid AreaId
        {
            get { return this.areaId; }
        }

        public string Name
        {
            get { return this.name; }
            set 
            {
                if(this.adapter.ChangeName(this.areaId, value))
                    this.name = value;
            }
        }
    }
}
